<?php
declare(strict_types=1);

namespace longbingcore\wxcore;


use app\Common\LongbingServiceNotice;

use app\shop\model\Express;
use think\facade\Db;

class Bird{

    static protected $uniacid;

    static protected $config;

    public function __construct($uniacid)
    {
       self::$uniacid = $uniacid;

       $config_model = new Express();

       self::$config = $config_model->dataInfo(['uniacid'=>$uniacid]);

    }


    /**
     * @param $order
     * @功能说明:查询物流信息
     * @author chenniang
     * @DataTime: 2021-04-08 16:39
     */
    function getOrderTracesByJson($order){

        $requestData['OrderCode']    = '';
        //物流公司
        $requestData['ShipperCode']  = $order['shipper_code'];
        //快递单号
        $requestData['LogisticCode'] = $order['express_id'];


        $requestData = json_encode($requestData);

        $datas = array(

            'EBusinessID' => self::$config['bird_id'],

            'RequestType' => '1002',

            'RequestData' => urlencode($requestData) ,

            'DataType'    => '2',

        );
        $datas['DataSign'] = $this->encrypt($requestData, self::$config['bird_key']);

        $result=$this->sendPost('http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);
        //根据公司业务处理返回的信息......
        return $result;
    }

    /**
     * @author chenniang
     * @DataTime: 2021-04-08 16:40
     * @功能说明:获取快递公司
     */
    public function getExpressCompany(){

        return [
            'SF'      =>'顺丰速运',
            'HTKY'    =>'百世快递',
            'ZTO'     =>'中通快递',
            'STO'     =>'申通快递',
            'YTO'     =>'圆通速递',
            'YD'      =>'韵达速递',
            'YZPY'    =>'邮政快递包裹',
            'EMS'     =>'EMS',
            'HHTT'    =>'天天快递',
            'JD'      =>'京东快递',
            'UC'      =>'优速快递',
            'DBL'     =>'德邦快递',
            'ZJS'     =>'宅急送',
            'TNT'     =>'TNT快递',
            'UPS'     =>'UPS',
            'DHL'     =>'DHL',
            'FEDEX'   =>'FEDEX联邦(国内件）',
            'FEDEX_GJ'=>'FEDEX联邦(国际件）',
            '0'       =>'平台未升级'
        ];
    }


    /**
     *  post提交数据
     * @param  string $url 请求Url
     * @param  array $datas 提交的数据
     * @return url响应返回的html
     */
    function sendPost($url, $datas) {
        $temps = array();
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);
        }
        $post_data = implode('&', $temps);
        $url_info = parse_url($url);
        if(empty($url_info['port']))
        {
            $url_info['port']=80;
        }
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader.= "Host:" . $url_info['host'] . "\r\n";
        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader.= "Connection:close\r\n\r\n";
        $httpheader.= $post_data;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
        $headerFlag = true;
        while (!feof($fd)) {
            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
                break;
            }
        }
        while (!feof($fd)) {
            $gets.= fread($fd, 128);
        }
        fclose($fd);

        return $gets;
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param appkey Appkey
     * @return DataSign签名
     */
    function encrypt($data, $appkey) {

        return urlencode(base64_encode(md5($data.$appkey)));
    }


}